package com.thkj.dao;

import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;


public interface TemMeasuringMapper extends Mapper<T> {

    /**
     * 查询 根据经营户
     *
     * @param worker_id
     * @return datediff(week, 时间字段,
     */
    @Select("SELECT temperature,state,DATE_FORMAT(measuring_time, '%Y-%m-%d %h:%i:%s')  as measuringTime " +
            "FROM  thkj_temperture_measuring " +
            "WHERE DATE_FORMAT(measuring_time,'%Y-%m-%d')= #{timeStr} " +
            "and worker_id = #{worker_id} " +
            "ORDER BY DATE(measuring_time) DESC ")
    @Results(value = {
            @Result(column = "temperature", property = "temperature", javaType = Double.class),
            @Result(column = "state", property = "state", javaType = Integer.class),
            @Result(column = "measuringTime", property = "measuringTime", javaType = String.class)
    })
    List<Map> queryByWorkerId(@Param("worker_id") String worker_id, @Param("timeStr") String timeStr);

    /**
     * 按年查看总数
     *
     * @param individual_id
     * @param state
     * @param year
     * @return
     */
    @Select("<script>" +
            "SELECT count(id) FROM thkj_temperture_measuring " +
            "WHERE individual_id = #{individual_id} " +
            "AND YEAR(measuring_time)= #{year} " +
            "<if test='state!=null'> " +
            "AND state = #{state} " +
            "</if> " +
            "<if test='state==null'> " +
            "AND (trim(state) != '') " +
            "</if> " +
            "</script>"
    )
    Integer queryByYearTotal(@Param("individual_id") String individual_id, @Param("state") Integer state, @Param("year") String year);

    /**
     * 按月查看总数
     *
     * @param individual_id
     * @param state
     * @param timeStr
     * @return
     */
    @Select("<script>" +
            "SELECT count(id) FROM thkj_temperture_measuring " +
            "WHERE individual_id = #{individual_id} " +
            "AND DATE_FORMAT(measuring_time,'%Y-%m')= #{timeStr} " +
            "<if test='state!=null'> " +
            "AND state = #{state} " +
            "</if> " +
            "<if test='state==null'> " +
            "AND (trim(state) != '') " +
            "</if> " +
            "</script>"
    )
    Integer queryByMonthTotal(@Param("individual_id") String individual_id, @Param("state") Integer state, @Param("timeStr") String timeStr);

    /**
     * 按天查看总数
     *
     * @param individual_id
     * @param state
     * @param timeStr
     * @return
     */
    @Select("<script>" +
            "SELECT count(id) FROM thkj_temperture_measuring " +
            "WHERE individual_id = #{individual_id} " +
            "AND DATE_FORMAT(measuring_time,'%Y-%m-%d')= #{timeStr} " +
            "<if test='state!=null'> " +
            "AND state = #{state} " +
            "</if> " +
            "<if test='state==null'> " +
            "AND (trim(state) != '') " +
            "</if> " +
            "</script>"
    )
    Integer queryByDayTotal(@Param("individual_id") String individual_id, @Param("state") Integer state, @Param("timeStr") String timeStr);

    /**
     * 查看本周总数
     *
     * @param individual_id
     * @param state
     * @param timeStr
     * @return
     */
    @Select("<script>" +
            "SELECT count(id) FROM thkj_temperture_measuring " +
            "WHERE individual_id = #{individual_id} " +
            "AND YEARWEEK(date_format(measuring_time,'%Y-%m-%d')) = YEARWEEK(now()) " +
            "<if test='state!=null'> " +
            "AND state = #{state} " +
            "</if> " +
            "<if test='state==null'> " +
            "AND (trim(state) != '') " +
            "</if> " +
            "</script>"
    )
    Integer queryByWeekTotal(@Param("individual_id") String individual_id, @Param("state") Integer state, @Param("timeStr") String timeStr);

}

